String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
};
var myDate = new Date();
function SetCookie(name, value) {
    var Days = 7;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString()
}
function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]);
    return null
}
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString()
}
function isMobil(s) {
    var patrn = /^(1\d{10})$/;
    if (!patrn.exec(s)) return false;
    return true;
}
function isTel(s) {
    var patrn = /^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/;
    if (!patrn.exec(s)) return false;
    return true;
}
//提示信息
function showMask(text, img, time, url, target) {
    if (time == null || time == "") {
        time = 2000;
    }
    $.ui.showMask(text, img);
    window.setTimeout(function () {
        $.ui.hideMask();
        if (target != null && url != "") {
            $("#afui").append("<a class=\"tlink\" href='" + url + "' data-ignore=\"True\"></a>");
            $(".tlink").click();
        }
    },
    time);
}
tab();
function tab() {
    $("#customNav li").click(function() {
        var index = $(this).index();
        //console.log(index);
        $(this).parent().find(".Triangle").remove();
        $(this).prepend("<div class=\"Triangle\">&nbsp;</div>");
        $(this).parent().find("li").removeClass("curr");
        $(this).addClass("curr");
        var customlist = $("#customList>li");
        customlist.hide();
        customlist.eq(index).show();
    });
    $("#customListAddress .addressList .default").click(function() {
        $(".addressList .curr .smallradius").removeClass("yellow");
        $(".addressList .curr .smallradius").addClass("white");
        $(this).find(".smallradius").removeClass("white");
        $(this).find(".smallradius").addClass("yellow");
        showMask("默认值设置成功", "right", 1000);
    });
    $(".customListBook .addressList .default").click(function() {
        $(".addressList .curr .smallradius").removeClass("yellow");
        $(".addressList .curr .smallradius").addClass("white");
        $(this).find(".smallradius").removeClass("white");
        $(this).find(".smallradius").addClass("yellow");
    });
    $("#addressBookNav li").click(function() {
        var index = $(this).index();
        $(this).parent().find(".Triangle").remove();
        $(this).prepend("<div class=\"Triangle\">&nbsp;</div>");
        $(this).parent().find("li").removeClass("curr");
        $(this).addClass("curr");
        var customlist = $("#customListBook>li");
        customlist.hide();
        customlist.eq(index).show();
    });

    $("#siteSearchNav li").click(function() { 
        var index = $(this).index();
        $(this).parent().find(".Triangle").remove();
        $(this).prepend("<div class=\"Triangle\">&nbsp;</div>");
        $(this).parent().find("li").removeClass("curr");
        $(this).addClass("curr");
        if (index == 0) {
            $("#siteSearchKey").show();
            $("#siteSearchLocation").hide();
        } else {
            $("#siteSearchLocation").css("height", document.body.scrollHeight - 88-43 + "px");
            $("#siteSearchLocation iframe").css("height", document.body.scrollHeight - 88 - 43 + "px");
            $("#siteSearchKey").hide();
            $("#siteSearchLocation").show();
        }
    });
    $("#suggestNav li").click(function() {
        var index = $(this).index();
        $(this).parent().find(".Triangle").remove();
        $(this).prepend("<div class=\"Triangle\">&nbsp;</div>");
        $(this).parent().find("li").removeClass("curr");
        $(this).addClass("curr");
        if (index == 0) {
            $("#suggest1").show();
            $("#suggest2").hide();
        } else {
            $("#suggest1").hide();
            $("#suggest2").show();
        }
    });
    
}


//摇一摇获取省市区+地址
function GPS() {
    if (window.DeviceMotionEvent) {
		var SHAKE_THRESHOLD = 1000; //摇动的强度
		var last_update = 0;
		var x = y = z = last_x = last_y = last_z = 0;
        window.addEventListener('devicemotion', deviceMotionHandler, false);
        function deviceMotionHandler(eventData) {
            var acceleration = eventData.accelerationIncludingGravity;
            var curTime = new Date().getTime();
            if ((curTime - last_update) > 100) {
                var diffTime = curTime - last_update;
                last_update = curTime;
                x = acceleration.x;
                y = acceleration.y;
                z = acceleration.z;
                var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
                if (speed > SHAKE_THRESHOLD) {
                    baiduLocation();
                }
                last_x = x;
                last_y = y;
                last_z = z;
            }
        }
        
    } else {
        showMask("设置不支持定位", "info", 1500);
    }
}
function baiduLocation() {
    //百度定位
    $.ui.showMask("定位中...", "info");
    htmlGPS2();

    function htmlGPS2() {
        var gc = new BMap.Geocoder();
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function(r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                gc.getLocation(r.point,
                    function(rs) {
                        var addComp = rs.addressComponents;
                        var Geolocationtext = addComp.province.replace("市", "") + "-" + addComp.city + "-" + addComp.district;
                        $.ui.hideMask();
                        $(".Sender").val(Geolocationtext);
                        $(".address").val(addComp.street + addComp.streetNumber);
                    });
            }
           else if (this.getStatus() == BMAP_STATUS_SERVICE_UNAVAILABLE) {
                $.ui.hideMask();
                showMask("服务不可用", "info", 1500);

           } else if (this.getStatus() == BMAP_STATUS_INVALID_REQUEST) {
               $.ui.hideMask();
               showMask("非法请求", "info", 1500); 
           }
           else if (this.getStatus() == BMAP_STATUS_PERMISSION_DENIED) {
               $.ui.hideMask();
               showMask("您拒绝了共享位置，可手动选择地区", "info", 1500);
           }
           else if (this.getStatus() == BMAP_STATUS_TIMEOUT) {
               $.ui.hideMask();
               showMask("定位超时", "info", 1500);
           }
            else {
                showMask("定位错误", "info", 1500);
                $.ui.hideMask();
            }
        },
            {
                enableHighAccuracy: true
            });
    }
}

//收件人省市区
function baiduLocationById(sender, address) {
    //百度定位
    $.ui.showMask("定位中...", "info");
    htmlGPS2();

    function htmlGPS2() {
        var gc = new BMap.Geocoder();
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function (r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                gc.getLocation(r.point,
                    function (rs) {
                        var addComp = rs.addressComponents;
                        //edite wangchun 2015-08-07  过滤地区中"市辖区"字符串
                        // var Geolocationtext = addComp.province.replace("市", "") + "-" + addComp.city + "-" + addComp.district;
                        //地区
                        var dst = "";
                        //如果地区为"市辖区" 就取城市
                        if (dst.trim() == "市辖区") {
                            dst = addComp.city;
                        } else {
                            dst = addComp.district.replace("市辖区", "");
                        }

                        var Geolocationtext = addComp.province.replace("市", "") + "-" + addComp.city + "-" + dst;
                        //end 2015-08-07 

                        $.ui.hideMask();
                        $("#" + sender).val(Geolocationtext);
                        $("#" + address).val(addComp.street + addComp.streetNumber);
                    });
            }
            else if (this.getStatus() == BMAP_STATUS_SERVICE_UNAVAILABLE) {
                $.ui.hideMask();
                showMask("服务不可用", "info", 1500);

            } else if (this.getStatus() == BMAP_STATUS_INVALID_REQUEST) {
                $.ui.hideMask();
                showMask("非法请求", "info", 1500);
            }
            else if (this.getStatus() == BMAP_STATUS_PERMISSION_DENIED) {
                $.ui.hideMask();
                showMask("您拒绝了共享位置，可手动选择地区", "info", 1500);
            }
            else if (this.getStatus() == BMAP_STATUS_TIMEOUT) {
                $.ui.hideMask();
                showMask("定位超时", "info", 1500);
            }
            else {
                showMask("定位错误", "info", 1500);
                $.ui.hideMask();
            }
        },
            {
                enableHighAccuracy: true
            });
    }
}


//加载省份城市数据页面
//id:省份城市页面ID;

function loadPovincePage(id,pageID){
    var ro = myDate.getMilliseconds();
    $.ui.loadContent('#provinceChange', false, false, 'up');
	SetCookie("loadPovincePageID",id);//写入input的ID
	SetCookie("pageID",pageID);//返回时需要跳转的panel ID
}

function loadPovinceOrderPage(id, pageID) {
    var ro = myDate.getMilliseconds();
    $.ui.loadContent('#provinceChangeOrder', false, false, 'up');
    SetCookie("loadPovincePageID", id);//写入input的ID
    SetCookie("pageID", pageID);//返回时需要跳转的panel ID
}

function selectAddress(id, pageID, pageName) {
    //console.log(pageName);
    var ro = myDate.getMilliseconds();
    $.ui.loadContent('#selectAddres', false, false, 'up');
    SetCookie("loadPovincePageID", id);//写入input的ID
    SetCookie("pageID", pageID);//返回时需要跳转的panel ID
}

function doAjaxAt(url, t, id) {
    //清除所有已加载的内容
     $.ui.loadContent('#' + id + '', false, false, 'pop');
    $.ajax({
        url: url,
        beforeSend: function() {
            $.ui.showMask("加载中，请稍后...");
        },
        success: function(data) {
            $.ui.hideMask();
            data = data.replace("<script", "<" + "script").replace("</script>", "<" + "/script>");
            if ($('#' + id + ' .afScrollPanel').length > 0) {
                $('#' + id + ' .afScrollPanel').html(data);
            } else {
                $('#' + id + '').html(data);
            }
			$.parseJS(data);
			//console.log($("#Sender").length);
			$(".Sender").val(getCookie("Sender"));
		    $(".Receiver").val(getCookie("Receiver"));  
			if(id=="priceresult")
			{
				$("#pSender").html(getCookie("Sender"));
		   		$("#pReceiver").html(getCookie("Receiver"));
			}
        }
    });
    $.ui.loadContent('' + id + '', false, false, 'pop');
	tab();
}

function doAjaxAtOne(url, t, id) {
    var ro = myDate.getMilliseconds();
    $.ui.loadContent('#' + id + '', false, false, 'pop');
    $.ajax({
        url: webRoot + url + "?v=" + ro + "",
        beforeSend: function() {
            $.ui.showMask("加载中，请稍后...");
        },
        success: function(data) {
            data = data.replace("<script", "<" + "script").replace("</script>", "<" + "/script>");
            $.ui.hideMask();
            if ($('#' + id + ' .afScrollPanel').length > 0) {
                $('#' + id + ' .afScrollPanel').html(data);
            } else {
                $('#' + id + '').html(data);
            }
            $.parseJS(data);
			$(".Sender").val(getCookie("Sender"));
		    $(".Receiver").val(getCookie("Receiver"));  
        }
    });
    $.ui.loadContent('' + id + '', false, false, 'pop');
	tab();
}
//加载默认内容，解决刷新
//var url = window.location.href;
//var u = url.substring(url.indexOf("#") + 1, url.length);
//if (url.indexOf("#") > -1) {    
//    if (u != "index" && u != "provinceChangeOrder") {
//		  $.ajax({
//        url: webRoot + u+".html",
//        beforeSend: function() {
//            $.ui.showMask("加载中，请稍后...");
//        },
//        success: function(data) {
//            data = data.replace("<script", "<" + "script").replace("</script>", "<" + "/script>");
//            $.ui.hideMask();
//            if ($('#' + u + ' .afScrollPanel').length > 0) {
//                $('#' + u + ' .afScrollPanel').html(data);
//            } else {
//                $('#' + u + '').html(data);
//            }
//            $.parseJS(data);
//			$(".Sender").val(getCookie("Sender"));
//		    $(".Receiver").val(getCookie("Receiver"));  
//			if(u=="priceresult")
//			{
//				$("#pSender").html(getCookie("Sender"));
//		   		$("#pReceiver").html(getCookie("Receiver"));
//			}
//        }
//     });
//   } 
//}
 